//
//  RNOLMoneyManagerTitleView.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/9.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLMoneyManagerTitleView.h"

@interface RNOLMoneyManagerTitleView()

@property (nonatomic,strong) UIButton *leftButton;
@property (nonatomic,strong) UIButton *righttButton;
@property (nonatomic,strong) UIButton *tempButton;
@property (nonatomic,strong) UIView *lineIndicatorView;

@end


@implementation RNOLMoneyManagerTitleView

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.titleLabel.font = KFontSize(17);
        [_leftButton setTitle:@"投资理财" forState:UIControlStateNormal];
        [_leftButton setTitle:@"投资理财" forState:UIControlStateSelected];
        [_leftButton setTitleColor: [KWhiteColor colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
        [_leftButton setTitleColor:  KWhiteColor  forState:UIControlStateSelected];
        _leftButton.tag = 0;
        [_leftButton addTarget:self action:@selector(clickIndexAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)righttButton{
    if (!_righttButton) {
        _righttButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _righttButton.titleLabel.font = KFontSize(17);
        [_righttButton setTitle:@"债权转让" forState:UIControlStateNormal];
        [_righttButton setTitle:@"债权转让" forState:UIControlStateSelected];
        [_righttButton setTitleColor: [KWhiteColor colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
        [_righttButton setTitleColor:  KWhiteColor  forState:UIControlStateSelected];
        _righttButton.tag = 1;
        [_righttButton addTarget:self action:@selector(clickIndexAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _righttButton;
}


- (UIView *)lineIndicatorView{
    if (!_lineIndicatorView) {
        _lineIndicatorView = [[UIView alloc] init];
        _lineIndicatorView.backgroundColor = KWhiteColor;
    }
    return _lineIndicatorView;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview: self.leftButton];
        [self addSubview: self.righttButton];
        [self addSubview: self.lineIndicatorView];
        
        self.leftButton.selected = YES;
        self.tempButton = self.leftButton;
        
    }
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat leftX = 0 ;
    CGFloat leftY = 0 ;
    CGFloat leftW = (self.width - 20)/2.0 ;
    CGFloat leftH = kNavBarHeight - kStatusBarHeight - 2 ;
    self.leftButton.frame = CGRectMake(leftX,leftY, leftW , leftH);
    
    CGFloat rightX = CGRectGetMaxX(self.leftButton.frame) + 10 ;
    CGFloat rightY = 0;
    CGFloat rightW = leftW;
    CGFloat rightH = leftH;
    self.righttButton.frame = CGRectMake(rightX, rightY, rightW , rightH);
    
    
    CGFloat lineX = 0;
    CGFloat lineY = CGRectGetMaxY(self.leftButton.frame) - 5 ;
    CGFloat lineW = leftW - 30;
    CGFloat lineH = 2;
    self.lineIndicatorView.frame = CGRectMake(lineX, lineY, lineW , lineH);
    self.lineIndicatorView.centerX = self.leftButton.centerX;
    
}

//设置默认的下标
- (void)scrollSelectIndex:(NSInteger)index{
    [self clickIndexAction:@[self.leftButton,self.righttButton][index]];
}

- (void)clickIndexAction:(UIButton *)button{
    if (self.tempButton == button) {
        return ;
    }
    NSInteger index = button.tag;
    [UIView animateWithDuration:0.25 animations:^{
     self.lineIndicatorView.centerX = button.centerX;
    }];
    self.tempButton.selected = NO;
    button.selected = YES;
    self.tempButton = button;
    !self.indexActionBlock? : self.indexActionBlock(index);
}


@end
